/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package prepocessing_2.com.prep;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

/**
 *
 * @author baga
 */
public class PostingList {

    private Connection conn = null;
    private ArrayList docPost = null;
    private ArrayList tokenPost = null;

    public PostingList() {
        docPost = new ArrayList();
        tokenPost = new ArrayList();
    }

    /*
     * metode ini bertujuan membuang/dump blok-blok posting list ke dalam.
     * database
     * input:
     *  HashMap
     * output :
     *  void
     */
//    public void writeBlockToDisk(String[] docs, String[] tokens, String doc) {
//        try {
//            String url = "jdbc:mysql://localhost:3306/xsedb";
//            Class.forName("com.mysql.jdbc.Driver");
//            conn = DriverManager.getConnection(url,
//                    "root", "");
//            PreparedStatement prepState = null;
//            ResultSet rs = null;
//            int much = 0;
//            String tempDoc = null;
//            for (int i = 0; i < docs.length; i++) {
////                System.out.println("token yang di select " + tokens[i]);
//                prepState = conn.prepareStatement("select count(*) from "
//                        + "dic_post where dictionary = ?");
//                prepState.setString(1, tokens[i]);
//                rs = prepState.executeQuery();
//                while (rs.next()) {
//                    much = rs.getInt(1);
//                }
//                if (much > 0) {
////                    System.out.println(tokens[i] + " select dictionary " + docs[i]);
//                    prepState = conn.prepareStatement("select posting from dic_post where " + "dictionary = ?");
//                    prepState.setString(1, tokens[i]);
//                    rs = prepState.executeQuery();
//                    while (rs.next()) {
//                        tempDoc = rs.getString(1);
//                    }
//                    prepState.clearParameters();
//                    tempDoc.concat("~" + docs[i]);
////                    System.out.println("update dictionary");
//                    prepState = conn.prepareStatement("update dic_post set posting = ? "
//                            + "where dictionary = ?");
//                    prepState.setString(1, tempDoc);
//                    prepState.setString(2, tokens[i]);
//                    prepState.execute();
//                    prepState.clearParameters();
//                } else {
//                    prepState = conn.prepareStatement("insert into "
//                            + "dic_post values(?, ?)");
//                    prepState.setString(1, tokens[i]);
//                    prepState.setString(2, docs[i]);
//                    prepState.execute();
//                    prepState.clearParameters();
//                }
//            }
//            int tDocId = 0;
//            PreparedStatement pstm = conn.prepareCall("select max(t_doc_id) "
//                    + "from t_doc");
//            rs = pstm.executeQuery();
//            while (rs.next()) {
//                tDocId = rs.getInt(1);
//            }
//            pstm.clearParameters();
//            pstm = conn.prepareCall("insert into t_doc "
//                    + "values(?,?,current_timestamp)");
//            pstm.setInt(1, ++tDocId);
//            pstm.setString(2, doc);
//            pstm.execute();
//            conn.close();
//            conn = null;
//            prepState = null;
//        } catch (SQLException se) {
//            se.printStackTrace();
//        } catch (ClassNotFoundException ce) {
//            ce.printStackTrace();
//        }
//    }

    /*
     * metode ini bertujuan mengambil posting list yang berada didalam database
     * input :
     *  null
     * output :
     *  void
     */
    public void getPostingList() {
        try {
            PreparedStatement prepStat = conn.prepareStatement("select * "
                    + "from dic_post");
            ResultSet rs = prepStat.executeQuery();
            while (rs.next()) {
                docPost.add(rs.getString("dictionary"));
                tokenPost.add(rs.getString("posting"));
            }
        } catch (SQLException se) {
            se.printStackTrace();
        }
    }

    public String[] getDocs() {
        return (String[]) docPost.toArray(new String[docPost.size()]);
    }

    public String[] getToken() {
        return (String[]) tokenPost.toArray(new String[tokenPost.size()]);
    }

    void writeBlockToDisk(String[] docs, String[] tokens, String doc, String[] docContent, String[] content) {
        try {
            String url = "jdbc:mysql://localhost:3306/xsedb";
            Class.forName("com.mysql.jdbc.Driver");
            conn = DriverManager.getConnection(url,
                    "root", "");
            PreparedStatement prepState = null;
            ResultSet rs = null;
            int much = 0;
            String tempDoc = null;
            for (int i = 0; i < docs.length; i++) {
//                System.out.println("token yang di select " + tokens[i]);
                prepState = conn.prepareStatement("select count(*) from "
                        + "dic_post where dictionary = ?");
                prepState.setString(1, tokens[i]);
                rs = prepState.executeQuery();
                while (rs.next()) {
                    much = rs.getInt(1);
                }
                if (much > 0) {
//                    System.out.println(tokens[i] + " select dictionary " + docs[i]);
                    prepState = conn.prepareStatement("select posting from dic_post where " + "dictionary = ?");
                    prepState.setString(1, tokens[i]);
                    rs = prepState.executeQuery();
                    while (rs.next()) {
                        tempDoc = rs.getString(1);
                    }
                    prepState.clearParameters();
                    tempDoc.concat("~" + docs[i]);
//                    System.out.println("update dictionary");
                    prepState = conn.prepareStatement("update dic_post set posting = ? "
                            + "where dictionary = ?");
                    prepState.setString(1, tempDoc);
                    prepState.setString(2, tokens[i]);
                    prepState.execute();
                    prepState.clearParameters();
                } else {
                    prepState = conn.prepareStatement("insert into "
                            + "dic_post values(?, ?)");
                    prepState.setString(1, tokens[i]);
                    prepState.setString(2, docs[i]);
                    prepState.execute();
                    prepState.clearParameters();
                }
            }
            int tDocId = 0;
            PreparedStatement pstm = conn.prepareCall("select max(t_doc_id) "
                    + "from t_doc");
            rs = pstm.executeQuery();
            while (rs.next()) {
                tDocId = rs.getInt(1);
            }
            pstm.clearParameters();
            pstm = conn.prepareCall("insert into t_doc "
                    + "values(?,?,current_timestamp)");
            pstm.setInt(1, ++tDocId);
            pstm.setString(2, doc);
            pstm.execute();
            PreparedStatement ps;
            for (int i = 0; i < docContent.length; i++) {
                ps = conn.prepareStatement("insert into doc_content "
                        + "values(?, ?)");
                ps.setString(1, docContent[i]);
                ps.setString(2, content[i]);
                ps.execute();
                ps.clearParameters();
            }
            conn.close();
            conn = null;
            prepState = null;
        } catch (SQLException se) {
            se.printStackTrace();
        } catch (ClassNotFoundException ce) {
            ce.printStackTrace();
        }
    }
}
